package com.example.client.controller;

import com.example.client.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @author: 默苍璃
 * @date: 2022-03-03 18:32
 */
@RestController
public class ClientController {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * getForObject 方法
     *
     * @return
     */
    @GetMapping("getForObject")
    public Object getForObject() {

        //请求URL地址
        String url = "http://localhost:8088/serverGet";

        //请求参数 可省略
        HashMap<Object, Object> map = new HashMap<>();

        //第二个参数为 请求请求接口 返回数据类型
        return restTemplate.getForObject(url, HashMap.class, map);
    }


    /**
     * getForEntity 方法
     *
     * @return
     */
    @GetMapping("getForEntity")
    public Object getForEntity() {

        //请求URL地址
        String url = "http://localhost:8088/serverGet";

        //请求参数 可省略
        HashMap<Object, Object> map = new HashMap<>();

        //第二个参数为 请求请求接口 返回数据类型  返回结果必须为 ResponseEntity
        ResponseEntity<HashMap> response = restTemplate.getForEntity(url, HashMap.class, map);

        //返回状态码包装类
        HttpStatus statusCode = response.getStatusCode();
        System.out.println(statusCode);

        //返回状态码
        int value = response.getStatusCodeValue();
        System.out.println(value);

        //返回 请求 头
        HttpHeaders headers = response.getHeaders();
        System.out.println(headers);

        //返回请求的结果
        return response.getBody();
    }


    /**
     * postForObject 方法
     *
     * @return
     */
    @GetMapping("postForObject")
    public User postForObject() {

        //请求URL地址
        String url = "http://localhost:8088/serverPost";

        //传入参数 post方法 必须为 MultiValueMap  传参  //使用User 传参也可以
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", "张三");
        map.add("age", 18);

        // url 参数 返回值
        User user = restTemplate.postForObject(url, map, User.class);

        return user;
    }


    /**
     * postForObject 方法 body 传参
     *
     * @return
     */
    @GetMapping("postForObjectBody")
    public User postForObjectBody() {

        //申明一个请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        //application / json
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        //请求URL地址
        String url = "http://localhost:8088/serverPostBody";

        //不可以使用 MultiValueMap 传参
        User user = new User();
        user.setAge(19);
        user.setName("历史");

        //使用 HttpEntity 包装参数   参数类型 class  请求头
        HttpEntity<User> httpEntity = new HttpEntity<User>(user, httpHeaders);

        // url 参数 返回值
        return restTemplate.postForObject(url, httpEntity, User.class);
    }


    /**
     * postForEntity 方法
     *
     * @return
     */
    @GetMapping("postForEntity")
    public User postForEntity() {

        //请求URL地址
        String url = "http://localhost:8088/serverPost";

        //传入参数 post方法 必须为 MultiValueMap  传参  //使用User 传参不可以 获取不到
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", "赵柳");
        map.add("age", 40);

        ResponseEntity<User> responseEntity = restTemplate.postForEntity(url, map, User.class);

        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getHeaders());

        return responseEntity.getBody();

    }


    /**
     * postForEntity 方法 body 传参
     *
     * @return
     */
    @GetMapping("postForEntityBody")
    public User postForEntityBody() {

        //请求URL地址
        String url = "http://localhost:8088/serverPostBody";

        User user = new User();
        user.setAge(20);
        user.setName("王五");

        ResponseEntity<User> responseEntity = restTemplate.postForEntity(url, user, User.class);

        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getHeaders());

        return responseEntity.getBody();

    }


    /**
     * exchange 方法
     *
     * @return
     */
    @GetMapping("exchange")
    public User exchange() {

        //请求URL地址
        String url = "http://localhost:8088/serverPost";


        //传入参数 post方法 必须为 MultiValueMap  传参  //使用User 传参不可以 获取不到
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", "赵柳9999");
        map.add("age", 40);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map);

        ResponseEntity<User> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, User.class);

        System.out.println(exchange.getStatusCode());
        System.out.println(exchange.getStatusCodeValue());
        System.out.println(exchange.getHeaders());

        return exchange.getBody();

    }


    /**
     * exchange 方法
     *
     * @return
     */
    @GetMapping("exchangeBody")
    public User exchangeBody() {

        //body传参
        String url = "http://localhost:8088/serverPostBody";

        //不可以使用 MultiValueMap 传参
        User user = new User();
        user.setAge(19);
        user.setName("历史");

        //申明一个请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        //application / json   MediaType.APPLICATION_JSON = MediaType.parseMediaType("application/json; charset=UTF-8");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<User> httpEntity = new HttpEntity<>(user, httpHeaders);

        ResponseEntity<User> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, User.class);

        System.out.println(exchange.getStatusCode());
        System.out.println(exchange.getStatusCodeValue());
        System.out.println(exchange.getHeaders());

        return exchange.getBody();

    }

}
